import { createRouter, createWebHistory } from 'vue-router'
import routes from '@/router/routes'
import { getToken } from '@/utils/auth'
//引入 nprogress
import NProgress from 'nprogress' // 进度条
import 'nprogress/nprogress.css' // 引入样式
NProgress.configure({ showSpinner: false })

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {
  NProgress.start()

  const token = getToken()
  const isRequireAuth = to.meta.isRequireAuth
  if (token) {
    next()
  } else if (isRequireAuth) {
    next('/login')
  } else if (!isRequireAuth) {
    next()
  }
})

router.afterEach(to => {
  NProgress.done()
})

export default router
